<?php
/**
 * Utils function for this websites
 * @author HoanVX
 * @version: $Id: ctUtils.php 91 2015-05-14 13:16:33Z voxuanhoan@gmail.com $
 *
 */
class  ChotroiUtils {

	function __construct() {

	}

	public static function alertPopup($message) {
		echo "<script type='text/javascript'> alert('". $message . "');</script>";
	}

	/**
	 * Convert value is got from $_POST object which is null to blank value
	 * @param unknown $name
	 * @return $_POST[$name] if exists that value in $_POST[]
	 *         "" if not exist $name in $_POST[]
	 */
	public static function convertPostNullToBlank($name) {
		return isset($_POST[$name]) ? $_POST[$name] : "";
	}

	/**
	 * Check path URI of image is existence or not
	 * Purpose is convert form default image path of wordpress to custom path
	 * if image is exist in default dir, return default dir: '/wp-content/uploads/...
	 * if image is existed in custome dir, return custom dir; /uploads/post-images/...
	 * @param unknown $src
	 * @return return URI to image if image is existed, blank if image is not existed
	 */
	public static function getUploadImageURI($src) {

		// Neu param is blank or path is incorrect
		if (empty($src) || !strpos($src, "uploads") ) {
			return TEMPLATE_DIRECTORY_URI .'/images/no_image.jpg';
		}

		$shortImgPath = explode("/uploads", $src);

		if (file_exists(ABSPATH.'/wp-content/uploads/'.$shortImgPath[1])) {
			return $src;
		} else if (file_exists(DIR_UPLOAD_IMG.$shortImgPath[1])) {
			return ROOT_URI . 'uploads/post-images' . $shortImgPath[1];
		} else {
			return TEMPLATE_DIRECTORY_URI .'/images/no_image.jpg'; // Image is not exist, set default image
		}
	}

	/**
	 * Compare and merge 2 array if they are have some differnce value
	 * Only merge element which have similar key
	 * @param unknown $scr: first array
	 * @param unknown $desc: second array
	 * @param $ignore: all keys which you want to skip (not compare and merge)
	 * @return $desc
	 */
	public static function parseAndMerge($scr = array(), $desc=array()) {
		foreach ($scr as $keySrc => $valSrc) {
			foreach ($desc as $keyDes => $valDes) {
				if (strcmp($keyDes, $keySrc) == 0) {
					if ((is_array($valSrc) && is_array($desc) && array_diff($valDes, $valSrc)) // Compare image
							|| (is_string($valDes) && is_string($valSrc) && strcmp($valDes, $valSrc) != 0)) {

						$desc[$keyDes] = $valSrc;
					}
				}
			}
		}

		return $desc;
	}


	/**
	 * Get value in array by key
	 * @param unknown $src: array
	 * @param unknown $key: key
	 * @return unknown|string: value follow key
	 */
	public static function getValueByKey($src, $key) {
		if (array_key_exists($key, $src)) {
			return $src[$key];
		}
		return "";
	}

	/**
	 * Get attachment image source
	 * @param unknown $postId
	 * @param unknown $size: size of image (widthm height)
	 * @return
	 */
	public static function getAttachImage($postId, $size = array(90,106)) {
		$args = array(
				'order' 	=> 'ASC',
				'post_type' => 'attachment',
				'numberposts' => -1,
				'post_status' => null,
				'post_parent' => $postId
		);

		$attachments = get_posts( $args );
		if ( $attachments ) {
			return wp_get_attachment_image_src($attachments[0]->ID, $size);
		}

		return array();
	}
}

